package carcassonne.server.websocket.message;

import carcassonne.server.websocket.WebSocketMessageType;
import com.alibaba.fastjson.JSON;

public class MessageOut {

    public static final int DefaultHostID = -1;

    public int id;
    public int seq;
    public String type;
    public String path;
    public Object data;

    public MessageOut(int seq, String path, Object data) {
        id = DefaultHostID;
        this.seq = seq;
        this.path = path;
        this.data = data;
        type = WebSocketMessageType.Info;
    }

    public MessageOut(int seq, String path, String type, Object data) {
        id = DefaultHostID;
        this.seq = seq;
        this.path = path;
        this.type = type;
        this.data = data;
    }

    public MessageOut(int id, int seq, String path, String type, Object data) {
        this.id = id;
        this.seq = seq;
        this.path = path;
        this.type = type;
        this.data = data;
    }

    public String ToJsonString() {
        return JSON.toJSONString(this);
    }

    public static MessageOut Error(String message) {
        return new MessageOut(DefaultHostID, -1, "", WebSocketMessageType.Error, message);
    }

    public static MessageOut OK(int seq) {
        return new MessageOut(DefaultHostID, seq, "", WebSocketMessageType.OK, null);
    }

    public static MessageOut Service(String path, Object data) {
        return new MessageOut(DefaultHostID, -1, path, WebSocketMessageType.Service, data);
    }
}
